//called by pounce handler
//called by animal handler (every couple of minutes)
//called by birth handler
//called by volcano/meteor/etc handler

import java.util.Random;
import java.util.Vector;

public class DeathHandler {

	public static boolean DEBUG = false;
	public static void setDEBUG(boolean b) {DEBUG = b;}
	public static boolean DEBUGA = false;
	public static void setDEBUGA(boolean b) {DEBUGA = b;}
	public static boolean DEBUGB = false;
	public static void setDEBUGB(boolean b) {DEBUGB = b;}
	public static boolean DEBUGC = false;
	public static void setDEBUGC(boolean b) {DEBUGC = b;}
	private static Random rdeath = new Random();

	public static void killChecksOnAnimal(AnimalObject animal){
		animal.adjustAge(1);
		Vector<loc> newloc = new Vector<loc>();
		newloc = AreaProximityHandler.getLocationAreas(animal, 60);
		int i=0;

		if( TempMap.getTempHere( animal.getLocation() ) >= 250) {
			if(DEBUG)
				System.out.println("HEATHDEATH"); //test
			if (rdeath.nextInt(1000) < 500) {
				animal.adjustHealth(-20);
			}
			for(i = 0; i < newloc.size(); i++) {
				if(TempMap.getTempHere(newloc.get(i)) < TempMap.getTempHere(animal.getLocation())) {
					if(DEBUGC)
						System.out.println("migrating to more less hot");					MoveHandler.doMove(newloc.get(i), animal);
						if(animal.getLiving()){
							MoveHandler.doMove(newloc.get(i), animal);
							animal.setMigrate(true);
							animal.setDisposition(0);
							if(i > 10)
								break;
						}
				}
			}
		} else if (TempMap.getTempHere(animal.getLocation() ) <= -10) {
			if(DEBUG)
				System.out.println("COLDDEATH"); //test
			if (rdeath.nextInt(1000) < 100) {
				animal.adjustHealth(-10);
				animal.adjustEnergy(-10); //shivering
			}
			for(i = 0; i < newloc.size(); i++) {
				if(TempMap.getTempHere(newloc.get(i)) > TempMap.getTempHere(animal.getLocation())) {
					if(DEBUGC)
						System.out.println("migrating to less cold");
					if(animal.getLiving()) {
						animal.setMigrate(true);
						animal.setDisposition(0);
						if(i > 10)
							break;
					}
				}
			}
		} else if((TempMap.getTempHere( animal.getLocation()) >= 400) &&
				(TempMap.getTempHere( animal.getLocation()) < -45)) {
			doDeath(animal);
			if(DEBUG)
				System.out.println("instaheatdeath"); //test
		}
		if(SimulationConstants.sealevel > HeightMap.getHeightHere(animal.getLocation())) {
			animal.adjustHealth(-5);
			if(DEBUG)
				System.out.println("Drowning");
			for(i = 0; i < newloc.size(); i++){
				if(HeightMap.getHeightHere(newloc.get(i)) > HeightMap.getHeightHere(animal.getLocation()))
					if(SimulationConstants.sealevel < HeightMap.getHeightHere(newloc.get(i))) {
						if(DEBUGC)
							System.out.println("migrating to higher ground");
						if(animal.getLiving()) {

							MoveHandler.doMove(newloc.get(i), animal);
							animal.setMigrate(true);
							animal.setDisposition(0);
							if(i > 10)
								break;
						}

					}
			}
		}

		if(rdeath.nextInt(1000) == 1)
		{
			doDeath(animal);
			if(DEBUG) {
				System.out.println("random death"); //test
				System.out.println("randomly "+animal.getAge()); //test
			}
		}
		if( animal.getAge() >= animal.getMaxAge() ) 
		{
			if(rdeath.nextInt(1000) < 300) 
			{
				if(DEBUG)
					System.out.println("age death");
				doDeath(animal);
			}
		}
		//borrowed time. 
		//animal is starving, borrowed time increases animal deathrate
		//borrowed time decreases fitness
		//borrowed time decreases mass
		if(animal.getEnergy() >= 20 && animal.getBorrowedTime() > 1) {
			animal.adjustBorrowedTime(-1); //we ate something
			if(DEBUGA)
				System.out.println("decremented borrowed time "+animal.getBorrowedTime()); //test
		}
		if( animal.getEnergy() <= 10 ) {
			animal.adjustBorrowedTime(1); //we starve
			animal.adjustMass(-5); //we are wasting away
			if(DEBUGA)
				System.out.println("added to borrowed time "+animal.getBorrowedTime()); //test
		}
		if(animal.getEnergy() > 50) {
			animal.setBorrowedTime(0);
		}
		if(animal.getHealth() < 0) {
			doDeath(animal);
			if(DEBUGB)
				System.out.println("FATALITY"); //test
		}
		if(animal.getBorrowedTime() > 50){
			doDeath(animal);
			if(DEBUGB)
				System.out.println("starved"); //test
		}


	}

	public static void doDeath(AnimalObject animal){
		//System.out.println("An Animal Died QQ");//test
		animal.setLiving(false);
		animal.setFoodValue(animal.getMass()+animal.getEnergy());
		animal.setFoodMax(animal.getFoodValue());
	}

	public static void APOCALYPSE() {
		for(int i = 0; i < AnimalHandlerObject.getAnimalVector().size(); i++) {
			AnimalHandlerObject.getAnimalVector().get(i).setLiving(false);
			AnimalHandlerObject.getAnimalVector().get(i).setFoodValue(20);
			//this should remove everything from the handler
		}
	}
}
